#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
const int N = 1e4 + 10;
char f[N][N];
int T, p;
int x, y;
char dfs(int x, int y)
{
    if (f[x][y])
    {
        return f[x][y];
    }
    f[x][y] = '3';
    if (x == 0)
    {
        return f[x][y] = '1';
    }
    if (y == 0)
    {
        return f[x][y] = '2';
    }
    return  f[x][y] = dfs((x + y) % p, (x + y + y) % p);
}
int main()
{
    cin >> T >> p;
    while (T--)
    {
        cin >> x >> y;
        char ret = dfs(x, y);
        if (ret == '1')
        {
            cout << '1' << endl;
        }
        else if (ret == '2')
        {
            cout << '2' << endl;
        }
        else
        {
            cout << "error" << endl;
        }
    }
    return 0;
}